﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using Illusion;
using Illusion.CustomAttributes;
using Illusion.Extensions;
using Sound;
using UnityEngine;
using UnityEngine.Audio;

namespace Manager
{
	// Token: 0x02000319 RID: 793
	public sealed class Sound : Singleton<Sound>
	{
		// Token: 0x170001E8 RID: 488
		// (get) Token: 0x060013FB RID: 5115 RVA: 0x0008D058 File Offset: 0x0008B258
		// (set) Token: 0x060013FC RID: 5116 RVA: 0x0008D060 File Offset: 0x0008B260
		public static AudioMixer Mixer { get; private set; }

		// Token: 0x060013FD RID: 5117 RVA: 0x0008D068 File Offset: 0x0008B268
		public static GameObject PlayFade(GameObject fadeOut, AudioSource audio, float fadeTime = 0f)
		{
			if (fadeOut != null)
			{
				fadeOut.GetComponent<FadePlayer>().SafeProc(delegate(FadePlayer p)
				{
					p.Stop(fadeTime);
				});
			}
			GameObject gameObject = audio.gameObject;
			gameObject.AddComponent<FadePlayer>().Play(fadeTime);
			return gameObject;
		}

		// Token: 0x170001E9 RID: 489
		// (get) Token: 0x060013FE RID: 5118 RVA: 0x0008D0C0 File Offset: 0x0008B2C0
		public AudioListener AudioListener
		{
			get
			{
				return this.listener.GetComponent<AudioListener>();
			}
		}

		// Token: 0x170001EA RID: 490
		// (get) Token: 0x060013FF RID: 5119 RVA: 0x0008D0D0 File Offset: 0x0008B2D0
		// (set) Token: 0x06001400 RID: 5120 RVA: 0x0008D0D8 File Offset: 0x0008B2D8
		public Transform Listener
		{
			get
			{
				return this._listener;
			}
			set
			{
				this._listener = value;
			}
		}

		// Token: 0x170001EB RID: 491
		// (get) Token: 0x06001401 RID: 5121 RVA: 0x0008D0E4 File Offset: 0x0008B2E4
		// (set) Token: 0x06001402 RID: 5122 RVA: 0x0008D0EC File Offset: 0x0008B2EC
		public GameObject currentBGM
		{
			get
			{
				return this._currentBGM;
			}
			set
			{
				if (this.oldBGM != null)
				{
					UnityEngine.Object.Destroy(this.oldBGM);
				}
				this.oldBGM = this._currentBGM;
				this._currentBGM = value;
			}
		}

		// Token: 0x06001403 RID: 5123 RVA: 0x0008D120 File Offset: 0x0008B320
		public void Register(AudioClip clip)
		{
			this.useAudioClipList.Add(clip);
		}

		// Token: 0x06001404 RID: 5124 RVA: 0x0008D130 File Offset: 0x0008B330
		public void Remove(AudioClip clip)
		{
			if (!this.useAudioClipList.Remove(clip) || this.useAudioClipList.Count((AudioClip p) => p == clip) == 0)
			{
				Resources.UnloadAsset(clip);
			}
		}

		// Token: 0x170001EC RID: 492
		// (get) Token: 0x06001405 RID: 5125 RVA: 0x0008D188 File Offset: 0x0008B388
		// (set) Token: 0x06001406 RID: 5126 RVA: 0x0008D190 File Offset: 0x0008B390
		public List<SoundSettingData.Param> settingDataList { get; private set; }

		// Token: 0x170001ED RID: 493
		// (get) Token: 0x06001407 RID: 5127 RVA: 0x0008D19C File Offset: 0x0008B39C
		// (set) Token: 0x06001408 RID: 5128 RVA: 0x0008D1A4 File Offset: 0x0008B3A4
		public List<Sound3DSettingData.Param> setting3DDataList { get; private set; }

		// Token: 0x06001409 RID: 5129 RVA: 0x0008D1B0 File Offset: 0x0008B3B0
		public Sound.OutputSettingData AudioSettingData(AudioSource audio, int settingNo)
		{
			if (settingNo < 0)
			{
				return null;
			}
			SoundSettingData.Param param = this.settingDataList[settingNo];
			if (param == null)
			{
				return null;
			}
			audio.volume = param.Volume;
			audio.pitch = param.Pitch;
			audio.panStereo = param.Pan;
			audio.spatialBlend = param.Level3D;
			audio.priority = param.Priority;
			audio.playOnAwake = param.PlayAwake;
			audio.loop = param.Loop;
			if (param.Setting3DNo >= 0)
			{
				Sound3DSettingData.Param param2 = this.setting3DDataList[param.Setting3DNo];
				if (param2 != null)
				{
					audio.dopplerLevel = param2.DopplerLevel;
					audio.spread = param2.Spread;
					audio.minDistance = param2.MinDistance;
					audio.maxDistance = param2.MaxDistance;
					audio.rolloffMode = (AudioRolloffMode)param2.AudioRolloffMode;
				}
			}
			return new Sound.OutputSettingData
			{
				delayTime = param.DelayTime
			};
		}

		// Token: 0x0600140A RID: 5130 RVA: 0x0008D2A4 File Offset: 0x0008B4A4
		public AudioSource Create(Sound.Type type, bool isCache = false)
		{
			GameObject gameObject = UnityEngine.Object.Instantiate<GameObject>(this.settingObjects[(int)type], (!isCache) ? this.typeObjects[(int)type] : this.ASCacheRoot, false);
			gameObject.SetActive(true);
			return gameObject.GetComponent<AudioSource>();
		}

		// Token: 0x0600140B RID: 5131 RVA: 0x0008D2E8 File Offset: 0x0008B4E8
		public Transform SetParent(Transform parent, LoadAudioBase script, GameObject settingObject)
		{
			GameObject gameObject = UnityEngine.Object.Instantiate<GameObject>(settingObject, parent, false);
			gameObject.SetActive(true);
			script.Init(gameObject.GetComponent<AudioSource>());
			return gameObject.transform;
		}

		// Token: 0x0600140C RID: 5132 RVA: 0x0008D318 File Offset: 0x0008B518
		public bool IsCheckParentCreated(Transform script)
		{
			return script.transform.parent != null && script.transform.parent.name == "notComp";
		}

		// Token: 0x0600140D RID: 5133 RVA: 0x0008D350 File Offset: 0x0008B550
		public void Bind(LoadSound script)
		{
			//if (script.audioSource == null)
			//{
			//	int type = (int)script.type;
			//	this.SetParent(this.typeObjects[type], script, this.settingObjects[type]);
			//}
			AudioSource audioSource = script.audioSource;
			audioSource.clip = script.clip;
			this.Register(script.clip);
			audioSource.name = script.clip.name;
			Sound.OutputSettingData outputSettingData = this.AudioSettingData(audioSource, script.settingNo);
			if (outputSettingData != null && script.delayTime <= 0f)
			{
				script.delayTime = outputSettingData.delayTime;
			}
		}

		// Token: 0x0600140E RID: 5134 RVA: 0x0008D3F0 File Offset: 0x0008B5F0
		public List<AudioSource> GetPlayingList(Sound.Type type)
		{
			List<AudioSource> list = new List<AudioSource>();
			Transform transform = this.typeObjects[(int)type];
			for (int i = 0; i < transform.childCount; i++)
			{
				list.Add(transform.GetChild(i).GetComponent<AudioSource>());
			}
			return list;
		}

		// Token: 0x0600140F RID: 5135 RVA: 0x0008D438 File Offset: 0x0008B638
		public bool IsPlay(Sound.Type type, string playName = null)
		{
			Transform transform = this.typeObjects[(int)type];
			for (int i = 0; i < transform.childCount; i++)
			{
				if (playName.IsNullOrEmpty() || !(playName != transform.GetChild(i).name))
				{
					return true;
				}
			}
			return false;
		}

		// Token: 0x06001410 RID: 5136 RVA: 0x0008D490 File Offset: 0x0008B690
		public bool IsPlay(Transform trans)
		{
			for (int i = 0; i < this.typeObjects.Length; i++)
			{
				if (this.IsPlay((Sound.Type)i, trans))
				{
					return true;
				}
			}
			return false;
		}

		// Token: 0x06001411 RID: 5137 RVA: 0x0008D4C8 File Offset: 0x0008B6C8
		public bool IsPlay(Sound.Type type, Transform trans)
		{
			Transform transform = this.typeObjects[(int)type];
			for (int i = 0; i < transform.childCount; i++)
			{
				Transform child = transform.GetChild(i);
				if (child == trans)
				{
					return true;
				}
			}
			return false;
		}

		// Token: 0x06001412 RID: 5138 RVA: 0x0008D50C File Offset: 0x0008B70C
		public Transform FindAsset(Sound.Type type, string assetName, string assetBundleName = null)
		{
			if (this.typeObjects == null)
			{
				return null;
			}
			Transform transform = this.typeObjects[(int)type];
			for (int i = 0; i < transform.childCount; i++)
			{
				Transform child = transform.GetChild(i);
				LoadAudioBase componentInChildren = child.GetComponentInChildren<LoadAudioBase>();
				if (componentInChildren != null && componentInChildren.clip != null && componentInChildren.assetName == assetName && (assetBundleName == null || componentInChildren.assetBundleName == assetBundleName))
				{
					if (type != Sound.Type.BGM)
					{
						return child;
					}
					if (child.gameObject != this.oldBGM)
					{
						return child;
					}
				}
			}
			return null;
		}

		// Token: 0x06001413 RID: 5139 RVA: 0x0008D5BC File Offset: 0x0008B7BC
		public Transform Play(Sound.Type type, string assetBundleName, string assetName, float delayTime = 0f, float fadeTime = 0f, bool isAssetEqualPlay = true, bool isAsync = true, int settingNo = -1, bool isBundleUnload = true)
		{
			LoadSound loadSound = new GameObject("Sound Loading").AddComponent<LoadSound>();
			loadSound.assetBundleName = assetBundleName;
			loadSound.assetName = assetName;
            //loadSound.type = type;
            loadSound.delayTime = delayTime;
			loadSound.fadeTime = fadeTime;
			loadSound.isAssetEqualPlay = isAssetEqualPlay;
			loadSound.isAsync = isAsync;
			loadSound.settingNo = settingNo;
			loadSound.isBundleUnload = isBundleUnload;
			return this.SetParent(this.typeObjects[(int)type], loadSound, this.settingObjects[(int)type]);
		}

		// Token: 0x06001414 RID: 5140 RVA: 0x0008D638 File Offset: 0x0008B838
		public void Stop(Sound.Type type)
		{
			List<GameObject> list = new List<GameObject>();
			Transform transform = this.typeObjects[(int)type];
			for (int i = 0; i < transform.childCount; i++)
			{
				list.Add(transform.GetChild(i).gameObject);
			}
			list.ForEach(delegate(GameObject p)
			{
				UnityEngine.Object.Destroy(p);
			});
		}

		// Token: 0x06001415 RID: 5141 RVA: 0x0008D6A0 File Offset: 0x0008B8A0
		public void Stop(Sound.Type type, Transform trans)
		{
			Transform transform = this.typeObjects[(int)type];
			for (int i = 0; i < transform.childCount; i++)
			{
				Transform child = transform.GetChild(i);
				if (child == trans)
				{
					UnityEngine.Object.Destroy(child.gameObject);
					return;
				}
			}
		}

		// Token: 0x06001416 RID: 5142 RVA: 0x0008D6F0 File Offset: 0x0008B8F0
		public void Stop(Transform trans)
		{
			for (int i = 0; i < this.typeObjects.Length; i++)
			{
				Transform transform = this.typeObjects[i];
				for (int j = 0; j < transform.childCount; j++)
				{
					Transform child = transform.GetChild(j);
					if (child == trans)
					{
						if (i == 0)
						{
							this.Stop(Sound.Type.BGM);
						}
						else
						{
							UnityEngine.Object.Destroy(child.gameObject);
						}
						return;
					}
				}
			}
		}

		// Token: 0x06001417 RID: 5143 RVA: 0x0008D768 File Offset: 0x0008B968
		public void PlayBGM(float fadeTime = 0f)
		{
			if (this.currentBGM != null)
			{
				this.currentBGM.GetComponent<FadePlayer>().SafeProc(delegate(FadePlayer p)
				{
					p.Play(fadeTime);
				});
			}
		}

		// Token: 0x06001418 RID: 5144 RVA: 0x0008D7B0 File Offset: 0x0008B9B0
		public void PauseBGM()
		{
			if (this.currentBGM != null)
			{
				this.currentBGM.GetComponent<FadePlayer>().SafeProc(delegate(FadePlayer p)
				{
					p.Pause();
				});
			}
		}

		// Token: 0x06001419 RID: 5145 RVA: 0x0008D7FC File Offset: 0x0008B9FC
		public void StopBGM(float fadeTime = 0f)
		{
			if (this.currentBGM != null)
			{
				this.currentBGM.GetComponent<FadePlayer>().SafeProc(delegate(FadePlayer p)
				{
					p.Stop(fadeTime);
				});
			}
			List<GameObject> list = (from item in this.typeObjects[0].gameObject.Children()
			where item != this.currentBGM
			select item).ToList<GameObject>();
			if (Sound.callback0 == null)
			{
				Sound.callback0 = new Action<GameObject>(UnityEngine.Object.Destroy);
			}
			list.ForEach(Sound.callback0);
		}

		// Token: 0x0600141A RID: 5146 RVA: 0x0008D898 File Offset: 0x0008BA98
		public AudioSource Play(Sound.Type type, AudioClip clip, float fadeTime = 0f)
		{
			AudioSource audioSource = this.Create(type, false);
			audioSource.clip = clip;
			audioSource.GetOrAddComponent<FadePlayer>().SafeProc(delegate(FadePlayer p)
			{
				p.Play(fadeTime);
			});
			return audioSource;
		}

		// Token: 0x0600141B RID: 5147 RVA: 0x0008D8DC File Offset: 0x0008BADC
		public AudioSource CreateCache(Sound.Type type, string bundle, string asset, string manifest = null)
		{
			Dictionary<string, AudioSource> dictionary;
			if (!this.dicASCache.TryGetValue((int)type, out dictionary))
			{
				Dictionary<string, AudioSource> dictionary2 = new Dictionary<string, AudioSource>();
				this.dicASCache[(int)type] = dictionary2;
				dictionary = dictionary2;
			}
			AudioSource audioSource;
			if (!dictionary.TryGetValue(asset, out audioSource))
			{
				audioSource = this.Create(type, true);
				audioSource.name = asset;
				audioSource.clip = new AssetBundleManifestData(bundle, asset, manifest).GetAsset<AudioClip>();
				this.Register(audioSource.clip);
				dictionary.Add(asset, audioSource);
			}
			return audioSource;
		}

		// Token: 0x0600141C RID: 5148 RVA: 0x0008D95C File Offset: 0x0008BB5C
		public void ReleaseCache(Sound.Type type, string bundle, string asset, string manifest = null)
		{
			Dictionary<string, AudioSource> dictionary;
			if (!this.dicASCache.TryGetValue((int)type, out dictionary))
			{
				return;
			}
			AudioSource audioSource;
			if (dictionary.TryGetValue(asset, out audioSource))
			{
				this.Remove(audioSource.clip);
				UnityEngine.Object.Destroy(audioSource.gameObject);
				dictionary.Remove(asset);
				AssetBundleManager.UnloadAssetBundle(bundle, false, manifest, false);
			}
			if (!dictionary.Any<KeyValuePair<string, AudioSource>>())
			{
				this.dicASCache.Remove((int)type);
			}
		}

		// Token: 0x0600141D RID: 5149 RVA: 0x0008D9D0 File Offset: 0x0008BBD0
		private void LoadSettingData()
		{
			this.settingDataList = new List<SoundSettingData.Param>();
			AssetBundleData assetBundleData = new AssetBundleData("sound/setting/soundsettingdata/00.unity3d", null);
			foreach (List<SoundSettingData.Param> collection in from p in assetBundleData.GetAllAssets<SoundSettingData>()
			select p.param)
			{
				this.settingDataList.AddRange(collection);
			}
			this.settingDataList.Sort((SoundSettingData.Param a, SoundSettingData.Param b) => a.No.CompareTo(b.No));
			assetBundleData.UnloadBundle(true, false);
			this.setting3DDataList = new List<Sound3DSettingData.Param>();
			assetBundleData = new AssetBundleData("sound/setting/sound3dsettingdata/00.unity3d", null);
			foreach (List<Sound3DSettingData.Param> collection2 in from p in assetBundleData.GetAllAssets<Sound3DSettingData>()
			select p.param)
			{
				this.setting3DDataList.AddRange(collection2);
			}
			this.setting3DDataList.Sort((Sound3DSettingData.Param a, Sound3DSettingData.Param b) => a.No.CompareTo(b.No));
			assetBundleData.UnloadBundle(true, false);
		}

		// Token: 0x0600141E RID: 5150 RVA: 0x0008DB54 File Offset: 0x0008BD54
		private void LoadSetting(Sound.Type type, int settingNo = -1)
		{
			string text = type.ToString();
			AssetBundleData assetBundleData = new AssetBundleData("sound/setting/object/00.unity3d", text.ToLower());
			GameObject gameObject = UnityEngine.Object.Instantiate<GameObject>(assetBundleData.GetAsset<GameObject>(), this.rootSetting, false);
			gameObject.name = text + "_Setting";
			AudioSource component = gameObject.GetComponent<AudioSource>();
			this.AudioSettingData(component, settingNo);
			if (text.CompareParts("gamese", true))
			{
			}
			this.settingObjects[(int)type] = gameObject;
			assetBundleData.UnloadBundle(true, false);
		}

		// Token: 0x0600141F RID: 5151 RVA: 0x0008DBDC File Offset: 0x0008BDDC
		protected override void Awake()
		{
			if (!base.CheckInstance())
			{
				return;
			}
			UnityEngine.Object.DontDestroyOnLoad(base.gameObject);
			Sound.Mixer = new AssetBundleData("sound/data/mixer/00.unity3d", "master").GetAsset<AudioMixer>();
			this.rootSetting = new GameObject("SettingObject").transform;
			this.rootSetting.SetParent(base.transform, false);
			this.rootPlay = new GameObject("PlayObject").transform;
			this.rootPlay.SetParent(base.transform, false);
			this.LoadSettingData();
			this.settingObjects = new GameObject[Utils.Enum<Sound.Type>.Length];
			this.typeObjects = new Transform[this.settingObjects.Length];
			for (int i = 0; i < this.settingObjects.Length; i++)
			{
				Sound.Type type = (Sound.Type)i;
				this.LoadSetting(type, -1);
				Transform transform = new GameObject(type.ToString()).transform;
				transform.SetParent(this.rootPlay, false);
				this.typeObjects[i] = transform;
			}
			this.dicASCache = new Dictionary<int, Dictionary<string, AudioSource>>();
			this.ASCacheRoot = new GameObject("AudioSourceCache").transform;
			this.ASCacheRoot.SetParent(this.rootPlay, false);
			this.listener = new GameObject("Listener", new System.Type[]
			{
				typeof(AudioListener)
			}).transform;
			this.listener.SetParent(base.transform, false);
			if (Camera.main != null)
			{
				this._listener = Camera.main.transform;
			}
		}

		// Token: 0x06001420 RID: 5152 RVA: 0x0008DD70 File Offset: 0x0008BF70
		private void Update()
		{
			if (this._listener != null)
			{
				this.listener.position = this._listener.position;
				this.listener.rotation = this._listener.rotation;
			}
			else
			{
				this.listener.position = Vector3.zero;
				this.listener.rotation = Quaternion.identity;
			}
		}

		// Token: 0x040018C2 RID: 6338
		private const string NotComplete = "notComp";

		// Token: 0x040018C4 RID: 6340
		[SerializeField]
		[NotEditable]
		private Transform _listener;

		// Token: 0x040018C5 RID: 6341
		[SerializeField]
		[NotEditable]
		private GameObject _currentBGM;

		// Token: 0x040018C6 RID: 6342
		[SerializeField]
		[NotEditable]
		private GameObject oldBGM;

		// Token: 0x040018C7 RID: 6343
		[NotEditable]
		[SerializeField]
		private Transform listener;

		// Token: 0x040018C8 RID: 6344
		[NotEditable]
		[SerializeField]
		private Transform rootSetting;

		// Token: 0x040018C9 RID: 6345
		[SerializeField]
		[NotEditable]
		private Transform rootPlay;

		// Token: 0x040018CA RID: 6346
		[SerializeField]
		[NotEditable]
		private Transform ASCacheRoot;

		// Token: 0x040018CB RID: 6347
		[NotEditable]
		[SerializeField]
		private Transform[] typeObjects;

		// Token: 0x040018CC RID: 6348
		[NotEditable]
		[SerializeField]
		private GameObject[] settingObjects;

		// Token: 0x040018CD RID: 6349
		private Dictionary<int, Dictionary<string, AudioSource>> dicASCache;

		// Token: 0x040018CE RID: 6350
		[SerializeField]
		[NotEditable]
		private List<AudioClip> useAudioClipList = new List<AudioClip>();

		// Token: 0x040018D3 RID: 6355
		[CompilerGenerated]
		private static Action<GameObject> callback0;

		// Token: 0x0200031A RID: 794
		public enum Type
		{
			// Token: 0x040018D9 RID: 6361
			BGM,
			// Token: 0x040018DA RID: 6362
			ENV,
			// Token: 0x040018DB RID: 6363
			SystemSE,
			// Token: 0x040018DC RID: 6364
			GameSE2D,
			// Token: 0x040018DD RID: 6365
			GameSE3D
		}

		// Token: 0x0200031B RID: 795
		public class OutputSettingData
		{
			// Token: 0x040018DE RID: 6366
			public float delayTime;
		}
	}
}
